package com.credithc.fastdfs.monitor.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.credithc.fastdfs.api.entity.FdfsInfo;
import com.credithc.fastdfs.monitor.config.SystemConfig;
import com.credithc.fastdfs.monitor.service.FdfsInfoService;
import com.credithc.framework.core.mybatis.page.PageController;
import com.credithc.framework.core.mybatis.page.PageDto;
import com.credithc.framework.core.utils.DataGrid;

/**
 * @ClassName: FileManageController
 * @Description: 文件管理页面
 * @author zhaoc
 * @date 2015年5月19日
 * @time 上午9:20:54
 */
@Controller
@RequestMapping(value = "/filemanage")
public class FileManageController {

	private static final Logger log = LoggerFactory
			.getLogger(FileManageController.class);

	@Resource(name = "fdfsInfoServiceImpl")
	public FdfsInfoService fdfsInfoServiceImpl;

	@RequestMapping(value = "listFileInfo")
	public ModelAndView fileManage() {
		ModelAndView mv = new ModelAndView("filemanage/listFileInfo");
		mv.addObject("downloadUrl", SystemConfig.FILE_DOWNLOAD_URL);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = "listFileInfoToJson")
	public DataGrid listFileManage(PageDto pageDto, FdfsInfo info,
			@RequestParam("sort") String sort,
			@RequestParam("order") String order, HttpServletRequest request, 
			HttpServletResponse response) {
		PageController pc = pageDto.createPage();
		pc.setSort(sort);
		pc.setOrder(order);
		return DataGrid.formateList(fdfsInfoServiceImpl.listFdfsInfo(pc, info));
	}

	@RequestMapping(value = "openDownloadWindow")
	public ModelAndView openDownloadWindow(@RequestParam("fsId") String fsId) {
		ModelAndView mv = new ModelAndView("filemanage/downloadfile");
		String url = SystemConfig.FILE_DOWNLOAD_URL;
		mv.addObject("downloadUrl", url + "?fsId=" + fsId);
		return mv;
	}
}
